home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_planetclouds.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.7 KB  |  111 lines

  1. /*
  2.  * planetclouds.sl - surface for a semi-opaque cloud layer to be put on
  3.  *                   an earth-like planetary model.
  4.  *
  5.  * DESCRIPTION:
  6.  *      When put on a sphere, sets the color & opacity of the sphere to
  7.  *   make it look like the clouds surrounding an Earth-like planet.
  8.  *      The shader works by creating a fractal turbulence function over
  9.  *   the surface, then modulating the opacity based on this function in
  10.  *   a way that looks like clouds on a planetary scale.
  11.  *
  12.  *
  13.  * PARAMETERS:
  14.  *    Ka, Kd - the usual meaning
  15.  *    distortionscale - controls the amount of texture distortion
  16.  *    omega,lambda,octaves - the fractal characteristics of the clouds
  17.  *    p4 - beats me
  18.  *    offset - controls the zero crossings (where the clouds disappear)
  19.  *
  20.  *
  21.  * HINTS:
  22.  *   1. The way this shader is typically used is to have two concentric
  23.  *      spheres represent a planet.  The inner one is colored like the
  24.  *      surface of a planet (perhaps using the "terran" shader), and the
  25.  *      outer one uses the "planetclouds" shader.
  26.  *   2. The best effects are achieved when the clouds not only occlude
  27.  *      the view of the planet, but also shadow it.  The way to do this
  28.  *      with the Blue Moon Renderer is to let the light cast shadows,
  29.  *      then declare the cloud sphere as follows:
  30.  *           AttributeBegin
  31.  *             Attribute "render" "casts_shadows" "shade"
  32.  *             Surface "planetclouds"
  33.  *             Sphere 1 -1 1 360
  34.  *           AttributeEnd
  35.  *   3. The default values for the shader assume that the planet is
  36.  *      represented by a unit sphere.  The texture space and/or parameters
  37.  *      to this shader will need to be altered if the size of your planet
  38.  *      is radically different.
  39.  *
  40.  *
  41.  * AUTHOR: Ken Musgrave
  42.  *    Conversion to Shading Language and other minor changes by Larry Gritz.
  43.  *
  44.  * REFERENCES:
  45.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  46.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  47.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  48.  *
  49.  * HISTORY:
  50.  *    ???? - original texture developed by Ken Musgrave.
  51.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  52.  *
  53.  * last modified 1 March 1994 by lg
  54.  */
  55.  
  56. #define TWOPI (2*PI)
  57.  
  58. /* Use signed Perlin noise */
  59. #define snoise(x) ((2*noise(x))-1)
  60. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  61. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  62. #define VERY_SMALL 0.001
  63.  
  64.  
  65.  
  66. surface k3d_planetclouds(float Ka = 0.5, Kd = 0.75;
  67.              float distortionscale = 1; float omega = 0.7;
  68.              float lambda = 2; float octaves = 9;
  69.              float offset = 0;)
  70. {
  71.   point Pdistortion;        /* "distortion" vector */
  72.   point PP;            /* Point after distortion */
  73.   float l, o, a, i;        /* Loop control for fractal sum */
  74.   float result;            /* Fractal sum is stored here */
  75.  
  76.   /* Transform to texture coordinates */
  77.   PP = transform("shader", P);
  78.  
  79.   /* Add in "distortion" vector */
  80.   Pdistortion = distortionscale * DNoise(PP);
  81.   /* Second cirrus: replace DNoise with vector fBm */
  82.   PP = PP + Pdistortion;
  83.  
  84.   /* Compute VLfBm */
  85.   l = 1;
  86.   o = 1;
  87.   a = 0;
  88.   for(i = 0; i < octaves && o >= VERY_SMALL; i += 1)
  89.     {
  90.       a += o * VLNoise(PP * l, 1);
  91.       l *= lambda;
  92.       o *= omega;
  93.     }
  94.   result = a;
  95.  
  96.   /* Adjust zero crossing (where the clouds disappear) */
  97.   result += offset;
  98.  
  99.   if(result < 0)
  100.     result = 0;
  101.  
  102.   /* Scale density */
  103.   result /= (1 + offset);
  104.  
  105.   /* Modulate surface opacity by the cloud value */
  106.   Oi = result * Os;
  107.  
  108.   /* Shade like matte, but with color scaled by cloud opacity */
  109.   Ci = Oi * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  110. }
  111.